package com.sgcc.nfc.lock.presentation.warnings

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.sgcc.nfc.lock.databinding.FragmentWarningsBinding
import com.sgcc.nfc.lock.domain.model.Warning
import com.sgcc.nfc.lock.domain.model.WarningStatus
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WarningsFragment : Fragment() {

    private var _binding: FragmentWarningsBinding? = null
    private val binding get() = _binding!!
    private val viewModel: WarningsViewModel by viewModels()
    private val warningsAdapter = WarningsAdapter(::onWarningClicked)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentWarningsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.warningsRecycler.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = warningsAdapter
        }
        setupChipListeners()
        observeViewModel()
    }

    private fun setupChipListeners() {
        binding.statusChipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
            val status = when (checkedIds.firstOrNull()) {
                binding.chipPending.id -> 0
                binding.chipHandled.id -> 1
                else -> null
            }
            viewModel.loadWarnings(status)
        }
    }

    private fun observeViewModel() {
        viewModel.warnings.observe(viewLifecycleOwner) { warnings ->
            warningsAdapter.submitList(warnings)
            updateStatistics(warnings)
        }
        viewModel.isLoading.observe(viewLifecycleOwner) { binding.warningsLoading.isVisible = it }
        viewModel.error.observe(viewLifecycleOwner) { error ->
            error?.let { Snackbar.make(binding.root, it, Snackbar.LENGTH_LONG).show() }
        }
    }

    private fun updateStatistics(warnings: List<Warning>) {
        val pendingCount = warnings.count { it.status == WarningStatus.PENDING }
        val handledCount = warnings.count { it.status == WarningStatus.HANDLED }
        val totalCount = warnings.size

        binding.pendingCount.text = pendingCount.toString()
        binding.handledCount.text = handledCount.toString()
        binding.totalCount.text = totalCount.toString()
    }

    private fun onWarningClicked(warning: Warning) {
        val bundle = Bundle().apply { putLong("warningId", warning.warningId) }
        findNavController().navigate(com.sgcc.nfc.lock.R.id.warningDetailFragment, bundle)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
